<template>
    <view class="login" :style="'background: url('+ ba+') no-repeat;'">
        <!-- <view class="login_bg"> -->
            <!-- <img src="/static/login_banner.png" style="width: 100%;height:  23vh;position: absolute;"/>
            <u-image width="127rpx" height="125rpx" src="/static/login_logo.png" :lazy-load="true"
                     class="man_login"></u-image>
            <view style="font-size: 40rpx;font-weight: 600;color: #fff;padding-top: 20rpx;text-align: center;position: relative;">
                动态更新
            </view> -->
        <!-- </view> -->
        <view class="login-box">
            <view style="font-size: 35rpx;font-weight: 600;">微信的授权信息</view>
            <view style="line-height: 60rpx;font-size: 30rpx;color: #fff;">你的账号信息（昵称、头像、地区及性别）</view>
        </view>
        <view class="login_bg2">
            <!-- <img src="/static/Background2.png" style="width: 100%;height: 45vh;position: absolute;"/> -->
            <form @submit="setformId" report-submit="true">
                <button form-type="submit" open-type="getUserInfo" @getuserinfo="login" class="button">使用微信账号授权并登录
                </button>
				<!-- <button form-type="submit" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"  class="button">使用微信账号授权并登录
				</button> -->
            </form>
        </view>

    </view>
</template>
<script>
	import {ba1} from "../../assets/img/base1.js"
    import {getOpenid,openIdLogin,RegisterUser} from "../../api/common";
    export default {
        name: "login",
        data() {
            return {
				ba:ba1,
				globalData:{
					// appid:'wx0dffcfd8a6ce6856',
					// secret:'3cd03a0311c8d18c6d737c90f62d604e'
				}
			}
        },
        // onLoad(options) {
        // },
        methods: {
			getPhoneNumber: function(e) {
				console.log(e.detail.errMsg)
				console.log(e.detail.iv)
				console.log(e.detail.encryptedData)
			},
            login(res) {
                let self = this;
                if (res.target.errMsg === "getUserInfo:ok") {
                    uni.login({
						  provider: 'weixin',
						success: (res2) => {
							uni.getUserInfo({
								provider: 'weixin',
								success: (info) => {//这里请求接口
									console.log(res2);
									console.log(info);
									uni.setStorage({
									    key: 'userInfo',
									    data: info.userInfo,
									    success: function () {
									        console.log('success');
									    }
									});
									getOpenid(res2.code).then(res=>{
										console.log(res)
										if(res.code== 0){
											console.log(res)
											uni.setStorage({
											    key: 'getOpenid',
											    data: res.data,
											    success: function () {
											        console.log('success');
													self.judgmentUse(res.data)
											    }
											});
											
										}else {
											uni.showToast({title:res.message,icon:"none"});
										}
									}).catch(error=>{
										uni.showToast({
												title: error,
												icon:"none"
										});
										this.$u.route({type:'redirectTo',url:'pages/login/loginApply'})	
									})
								},
								fail: () => {
									uni.showToast({title:"微信登录授权失败",icon:"none"});
								}
							})
						},
						fail: () => {
							uni.showToast({title:"微信登录授权失败",icon:"none"});
						}
                    })
                }else {
					uni.showToast({
						title: '微信登录授权失败',
						icon:"none"
					});
				}
            }, 
			setformId(e) {
                this.formId = e.target.formId;
            },
			judgmentUse(e){
				let _that = this
				let use = {}
			
				openIdLogin(e.openid).then(res=>{
					if(res.code == 0){
						use = {
							name:res.data["姓名"],
							idcard:res.data["身份证号"],
							tel: res.data["手机号"],
							idcardType: res.data["身份类型"],
							busesNumber:  res.data["公交次数"],
							creditRating:  res.data["信用等级"],
							qrcode: res.data["生成二维码权限"],
							scan: res.data["核验二维码权限"]
						}
						uni.setStorage({
						    key: 'user',
						    data: use,
						    success: function () {
						        console.log('success');
								_that.$u.route({type:'navigateBack'})
						    }
						});
					}else{
						uni.showToast({
								title: res.message,
								icon:"none"
						});
					}
				}).catch(error=>{
					uni.showToast({
							title: '目前未申领崇军码',
							icon:"none"
					});
					uni.showModal({
					    title: '提示',
					    content:  `未领申码,前往注册页面`,
						cancelColor:'#a6a6a6',
						confirmColor:'#19BE6B',
					    success: function (re) {
							if (re.confirm) {
								console.log('用户点击确定');
							   _that.$u.route({type:'redirectTo',url:'pages/login/loginApply'})
							} else if (re.cancel) {
							   console.log('用户点击取消');
							}
					    }
					});
					// this.$u.route({type:'redirectTo',url:'pages/login/loginApply'})	
				})
			}
        },
        onShareAppMessage: function () {
            return {
                title: '转发',
                path: '/pages/home/index',
                success: function (res) {
                }
            }
        }
    };
</script>

<style scoped>
    .login {
        height: 100vh;
        background-color: #fff;
        position: relative;
		background-size:cover; 
    }

    .man_login {
        padding-top: 50rpx;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .login_bg {
        width: 100vw;
        height: 23vh;
    }

    .login-box {
   /*     -moz-box-shadow: 0px 2px 2px #d9d9d9;
        -webkit-box-shadow: 0px 2px 3px #d9d9d9;
        box-shadow: 0px 2px 3px #d9d9d9; */
        height: 27vh;
        padding: 70rpx;
		color: #FFFFFF;
    }

    .login_bg2 {
        width: 100%;
        height: 45vh;
    }

    .button {
        width: 495rpx;
        border-radius: 20rpx;
        font-size: 36rpx;
        font-weight: 600;
        color: #fff;
        position: relative;
        background-color: #ffc002;
        top: 105rpx;
        /* box-shadow: 0 0 15px #ababab; */
        padding: 10rpx;
        position: relative;
    }
</style>
